package com.gidgabs.modules.api.service.my;

import com.gidgabs.common.utils.Assist;
import com.gidgabs.common.utils.HuanXinUtil;
import com.gidgabs.common.utils.LangUtil;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.dto.TbUserDto;
import com.gidgabs.dto.TbUserIncomeDto;
import com.gidgabs.modules.dao.*;
import com.gidgabs.modules.entity.TbUser;
import com.gidgabs.modules.entity.TbUserIncomeEntity;
import com.gidgabs.utils.SessionDataUtil;
import com.gidgabs.utils.YunXinUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 我的模块
 * 用户信息相关
 * Created by tby on 2018/7/4.
 */
@Service
public class MyUserService {
    @Autowired private TbUserDao tbUserDao;
    @Autowired private TbUserFollowDao tbUserFollowDao;
    @Autowired private TbUserJournalDao tbUserJournalDao;
    @Autowired private TbProductDao tbProductDao;
    @Autowired private TbUserIncomeDao tbUserIncomeDao;
    @Autowired private HuanXinUtil huanXinUtil;

    /**
     * 查询用户详情
     * @param userId
     * @return
     */
    @Transactional(readOnly = true)
    public ReturnJson<TbUserDto> userDetail(Integer userId) {
        Integer shouldUserId=null;

        if (userId==null){
            shouldUserId=SessionDataUtil.getCurrentUserId();
        }else {
            shouldUserId=userId;

        }
        TbUserDto tbUserDto=tbUserDao.selectTbUserById(shouldUserId).toDto();
        Assist assist=new Assist();
        //获取关注数量
        assist.andEq("user_id_master",shouldUserId);
        int countFollow= (int) tbUserFollowDao.getTbUserFollowRowCount(assist);
        assist=new Assist();
        assist.andEq("source_user_id",shouldUserId);
        //日志转载数量
        int journalForwardCount= (int) tbUserJournalDao.getTbUserJournalRowCount(assist);
        //广告转载数量
        int advertiseForwardCount= (int) tbProductDao.getTbProductRowCount(assist);
        tbUserDto.setForwardCount(journalForwardCount+advertiseForwardCount);
        tbUserDto.setFollowCount(countFollow);
        return ReturnJson.success("查询成功",tbUserDto);
    }

    /**
     * 修改个人信息
     * @param tbUserDto
     * @return
     */
    @Transactional
    public ReturnJson<TbUserDto> deitUser(TbUserDto tbUserDto) {
        Integer currentUserId = SessionDataUtil.getCurrentUserId();
        TbUser tbUser = tbUserDao.selectTbUserById(currentUserId);
        tbUser.edit(tbUserDto);
        YunXinUtil.updateUserInfo(String.valueOf(tbUser.getUserId()),tbUser.getUserName(),tbUser.getHeadImgUrl(),tbUser.getAutograph(),tbUser.getEmail(),null,tbUser.getMobile(),tbUser.getGender());
        tbUserDao.updateNotNullTbUserById(tbUser);
        return ReturnJson.success("修改成功");
    }

    /**
     * 钱包统计
     * @return
     */
    public ReturnJson wallet() {
        Integer currentUserId = SessionDataUtil.getCurrentUserId();
        List<TbUserIncomeDto> tbUserIncomeDtos =tbUserIncomeDao.walletGroup(currentUserId);
        Map<String,Integer> result=new HashMap<>();
        for (TbUserIncomeDto tbUserIncomeDto:tbUserIncomeDtos){
            result.put(tbUserIncomeDto.getRevenueType(),tbUserIncomeDto.getMoney());
        }
        return ReturnJson.success("查询成功",result);
    }

    /**
     * 钱包明细记录
     * @param pageSize
     * @param pageNo
     * @param revenueType
     * @return
     */
    public ReturnJson<TbUserIncomeDto> walletList(Integer pageSize, Integer pageNo, String revenueType) {
        Assist assist=new Assist();
        assist.andEq("revenue_type",revenueType).andEq("user_id",SessionDataUtil.getCurrentUserId())
                .setOrder(Assist.order("create_time",false))
                .setStartRow(LangUtil.firstLimit(pageSize,pageNo))
                .setRowSize(pageSize);

        List<TbUserIncomeDto> incomeDtos = tbUserIncomeDao.selectTbUserIncomeEntity(assist)
                .stream()
                .map(TbUserIncomeEntity::toDto)
                .collect(Collectors.toList());

        long count = tbUserIncomeDao.getTbUserIncomeEntityRowCount(assist);
        return ReturnJson.success("查询成功",incomeDtos, (int) count);
    }
}
